package com.example.demo.arithmetic.algorithm.dynamic_programing;

public class Leetcode538 {
    /**
     * @param a a    b   c   x   y   z
     * @param b a    b   x   y   z
     * @return
     */
    public int longestCommonSubsequence(String a, String b) {
        char[] aCharArray = a.toCharArray();
        char[] bCharArray = b.toCharArray();
        int m = a.length();
        int n = b.length();

        int[][] dp = new int[n + 1][m + 1];
        for (int i = 1; i < n + 1; i++) {
            for (int j = 1; j < m + 1; j++) {
                if (bCharArray[i - 1] == aCharArray[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        printDp(dp);
        return m + n - 2 * dp[n][m];
    }

    private static void printDp(int[][] arr) {
        for (int[] its : arr) {
            for (int anInt : its) {
                System.out.print(anInt + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        //System.out.println(new LCSubsequence().longestCommonSubsequence("abcde", "ace"));
        System.out.println(new Leetcode538().longestCommonSubsequence("leetcode", "etco"));
    }
}
